<?php
/**
 * User: zwh
 * Time: 2016-10-24 14:36:12
 */
class MirrorOrders extends Eloquent
{
	protected $primaryKey = 'order_id';
    protected $table = "mirror_orders";
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-24 15:08:29
     * @Description: 创建保存镜像单信息
     */
    public static function saveMirrorOrders($order_id,$orders){
        return DB::transaction(function () use ($order_id,$orders) {
            $temp_uci=UserCarInfo::where("user_id",$orders->pay_user_id)->where("car_no",$orders->car_no)->where("status",1)->first();
            if($temp_uci){
                $temp_csi=DataCarStandardInfo::find($temp_uci->car_model_id);
            }            
            $userInfo=Users::find($orders->pay_user_id);
            if($orders->tittle=='女士'){
                $sex=2;
            }else{
                $sex=1;
            }
            $company_name="";
            $mirror_orders = new MirrorOrders();
            $mirror_order_id=Common::createOrderId();
            $mirror_orders->order_id=$mirror_order_id;
            $mirror_orders->status=$orders->status;
            $mirror_orders->name=$orders->name;
            $mirror_orders->phone=$orders->phone;
            $mirror_orders->sex=$sex;
            $mirror_orders->user_id=$userInfo->id;
            $mirror_orders->user_name=$userInfo->real_name;
            $mirror_orders->user_sex=$userInfo->sex;
            $mirror_orders->user_phone=$userInfo->phone;
            $mirror_orders->user_type=$userInfo->type;
            $mirror_orders->meet_time=$orders->created_at;
            if($userInfo->type==2){
                $compnay=Companys::find($orders->pay_user_id);
                $company_name=isset($compnay)?$compnay->name:"";
            }
            $mirror_orders->company_name=$company_name;
            $mirror_orders->car_brand_id=isset($temp_csi)?$temp_csi->brand_name_id:$orders->car_brand_id;
            $mirror_orders->car_brand_name=isset($temp_csi)?$temp_csi->brand_name:$orders->car_brand_name;
            $mirror_orders->car_series_id=isset($temp_csi)?$temp_csi->series_name_id:$orders->car_series_id;
            $mirror_orders->car_series_name=isset($temp_csi)?$temp_csi->series_name:$orders->car_series_name;
            $mirror_orders->car_model_id=isset($temp_csi)?$temp_csi->id:$orders->car_model_id;
            $mirror_orders->car_model_name=isset($temp_csi)?$temp_csi->model_name:$orders->car_model_name;
            $mirror_orders->amount=$orders->amount;
            $mirror_orders->discount_amount=$orders->discount_amount;
            $mirror_orders->payable_amount=$orders->payable_amount;
            $mirror_orders->balance_amount=$orders->balance_amount;
            $mirror_orders->discount_rate=$orders->discount_rate;
            $mirror_orders->discount=$orders->discount;
            $mirror_orders->shop_id=$orders->shop_id;
            $mirror_orders->car_no=$orders->car_no;
            $mirror_orders->mileage=$orders->mileage;
            $mirror_orders->city=$orders->city;
            $mirror_orders->pay_time=$orders->pay_time;
            $mirror_orders->order_type=$orders->order_type;
            $mirror_orders->relation_order=$order_id;
            $mirror_orders->engineer_id=$orders->engineer_id;
            $mirror_orders->save();
            
            $order_product=OrderProduct::where("order_id",$order_id)->where("status",1)->get();
            foreach ($order_product as $_op) {
                $mirror_op=new MirrorOrderProduct();
                $mirror_op->order_id=$mirror_order_id;
                $mirror_op->product_type=$_op->product_type;
                $mirror_op->product_id=$_op->product_id;
                $mirror_op->product_name=$_op->product_name;
                $mirror_op->status=$_op->status;
                $mirror_op->amount=$_op->amount;
                $mirror_op->engineer_id=$_op->engineer_id;
                $mirror_op->discount=$_op->discount;
                $mirror_op->counter_amount=$_op->counter_amount;
                $mirror_op->save();
                switch ($_op->product_type) {
                    case 'enterprise':
                        $opep=OrderProductEsPackage::where("order_product_id",$_op->id)->get();
                        foreach($opep as $_opep){
                            $mirror_opep=new MirrorOrderProductEsPackage();
                            $mirror_opep->order_id=$mirror_order_id;
                            $mirror_opep->order_product_id=$mirror_op->id;
                            $mirror_opep->package_id=$_opep->package_id;
                            $mirror_opep->package_name=$_opep->package_name;
                            $mirror_opep->work_station=$_opep->work_station;
                            $mirror_opep->work_status=$_opep->work_status;
                            $mirror_opep->amount=$_opep->amount;
                            $mirror_opep->engineer_id=$_opep->engineer_id;
                            $mirror_opep->save(); 
                            $items=OrderProductEsItem::where('order_product_package_id',$mirror_opep->id)->get();
                            foreach($items as $item){
                                $mirror_opei=new MirrorOrderProductEsItem();
                                $mirror_opei->order_id=$mirror_order_id;
                                $mirror_opei->order_product_package_id=$mirror_opep->id;
                                $mirror_opei->item_id=$item->item_id;
                                $mirror_opei->item_name=$item->item_name;
                                $mirror_opei->amount=$item->amount;
                                $mirror_opei->num=$item->num;
                                $mirror_opei->type=$item->type;
                                $mirror_opei->item_type=$item->item_type;
                                $mirror_opei->unit=$item->unit;
                                $mirror_opei->counter_amount=$item->counter_amount;
                                $mirror_opei->save();
                            }   
                        }
                        break;
                    case 'activity':
                        $opacp=OrderProductAcPackage::where("order_product_id",$_op->id)->get();
                        foreach($opacp as $_opacp){
                            $mirror_opacp=new MirrorOrderProductAcPackage();
                            $mirror_opacp->order_id=$mirror_order_id;
                            $mirror_opacp->order_product_id=$mirror_op->id;
                            $mirror_opacp->package_id=$_opacp->package_id;
                            $mirror_opacp->package_name=$_opacp->package_name;
                            $mirror_opacp->work_station=$_opacp->work_station;
                            $mirror_opacp->work_status=$_opacp->work_status;
                            $mirror_opacp->amount=$_opacp->amount;
                            $mirror_opacp->engineer_id=$_opacp->engineer_id;
                            $mirror_opacp->save();
                            $items=OrderProductAcItem::where('order_product_package_id',$_opacp->id)->get();
                            foreach($items as $item){
                                $mirror_opei=new MirrorOrderProductAcItem();
                                $mirror_opei->order_id=$mirror_order_id;
                                $mirror_opei->order_product_package_id=$mirror_opacp->id;
                                $mirror_opei->item_id=$item->item_id;
                                $mirror_opei->item_name=$item->item_name;
                                $mirror_opei->amount=$item->amount;
                                $mirror_opei->num=$item->num;
                                $mirror_opei->type=$item->type;
                                $mirror_opei->item_type=$item->item_type;
                                $mirror_opei->unit=$item->unit;
                                $mirror_opei->discount=$item->discount;
                                $mirror_opei->counter_amount=$item->counter_amount;
                                $mirror_opei->save();
                            }
                        }
                        break;
                    case 'promotion':
                       $opp=OrderProductPackage::where("order_product_id",$_op->id)->get();
                        foreach($opp as $_opp){
                            $mirror_opp=new MirrorOrderProductEsPackage();
                            $mirror_opp->order_id=$mirror_order_id;
                            $mirror_opp->order_product_id=$mirror_op->id;
                            $mirror_opp->package_id=$_opp->package_id;
                            $mirror_opp->package_name=$_opp->package_name;
                            $mirror_opp->work_station=$_opp->work_station;
                            $mirror_opp->work_status=$_opp->work_status;
                            $mirror_opp->amount=$_opp->amount;
                            $mirror_opp->engineer_id=$_opp->engineer_id;
                            $mirror_opp->save(); 
                            $items=OrderProductItem::where('order_product_package_id',$opp->package_id)->get();
                            foreach($items as $item){
                                $mirror_opi=new MirrorOrderProductAcItem();
                                $mirror_opi->order_id=$mirror_order_id;
                                $mirror_opi->order_product_package_id=$mirror_opp->id;
                                $mirror_opi->item_id=$item->item_id;
                                $mirror_opi->item_name=$item->item_name;
                                $mirror_opi->amount=$item->amount;
                                $mirror_opi->num=$item->num;
                                $mirror_opi->type=$item->type;
                                $mirror_opi->item_type=$item->item_type;
                                $mirror_opi->unit=$item->unit;
                                $mirror_opi->counter_amount=$item->counter_amount;
                                $mirror_opi->save();
                            }   
                        }
                        break;
                    case 'base':
                        $opbi=OrderProductBaseItem::where("order_product_package_id",$_op->id)->get();
                        foreach($opbi as $_opbi){
                            $mirror_opbi=new MirrorOrderProductBaseItem();
                            $mirror_opbi->order_id=$mirror_order_id;
                            $mirror_opbi->order_product_package_id=$mirror_op->id;
                            $mirror_opbi->item_id=$_opbi->item_id;
                            $mirror_opbi->item_name=$_opbi->item_name;
                            $mirror_opbi->amount=$_opbi->amount;
                            $mirror_opbi->num=$_opbi->num;
                            $mirror_opbi->type=$_opbi->type;
                            $mirror_opbi->item_type=$_opbi->item_type;
                            $mirror_opbi->unit=$_opbi->unit;
                            $mirror_opbi->discount=$_opbi->discount;
                            $mirror_opbi->counter_amount=$_opbi->counter_amount;
                            $mirror_opbi->save();
                        }
                        break;
                    case 'non_standard':
                        $opns=OrderProductNonStandard::where("order_product_id",$_op->id)->where('order_product_non_standard.status',1)->get();
                        foreach ($opns as $_opns) {
                            $mirror_opns=new MirrorOrderProductNonStandard();
                            $mirror_opns->order_id=$mirror_order_id;
                            $mirror_opns->item_id=$_opns->item_id;
                            $mirror_opns->item_id=$_opns->item_id;
                            $mirror_opns->name=$_opns->name;
                            $mirror_opns->num=$_opns->num;
                            $mirror_opns->amount=$_opns->amount;
                            $mirror_opns->order_product_id=$mirror_op->id;
                            $mirror_opns->work_station=$_opns->work_station;
                            $mirror_opns->work_status=$_opns->work_status;
                            $mirror_opns->type=$_opns->type;
                            $mirror_opns->service_type=$_opns->service_type;
                            $mirror_opns->unit=$_opns->unit;
                            $mirror_opns->discount=$_opns->discount;
                            $mirror_opns->status=$_opns->status;
                            $mirror_opns->counter_amount=$_opns->counter_amount;
                            $mirror_opns->save();
                        }
                        break;
                }
            }
            return array('code' => 0, 'msg' => 'ok', 'result' => array('mirror_order_id'=>$mirror_order_id));
        });
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-8-31 09:40:47
     * @Description: 订单价格每次变动都需要重新计算
     */
    public static function calculate($order_id){
        return DB::transaction(function () use ($order_id) {
            $order = MirrorOrders::where("order_id",$order_id)->lockForUpdate()->first();
            $order_product=MirrorOrderProduct::where("order_id",$order_id)->where("status",1)->lockForUpdate()->get();
    
            //计算产品包
            $order_discount=0;//订单总优惠
            $order_amount=0;//订单总金额
            foreach ($order_product as $_op) {
                $order_product_discount=0;//单个产品包优惠金额
                $order_product_amount=0;//单个产品包金额
                switch ($_op->product_type) {
                    case 'enterprise':
                        $opep=MirrorOrderProductEsPackage::where("order_product_id",$_op->id)->get();
                        foreach($opep as $_opep){
                            $package_amount=0;//企业包中单个包的价格
                            $item=MirrorOrderProductEsItem::where('order_product_package_id',$_opep->id)->get();
                            foreach ($item as $key=>$_item) {
                                if($_item->type==1){
                                    $package_amount+=$_item->amount*$_item->num;
                                    $order_product_amount+=$_item->amount*$_item->num;
                                }else{
                                    $package_amount+=$_item->amount;
                                    $order_product_amount+=$_item->amount;
                                }
                            }
                            $_opep->amount=$package_amount;
                            $_opep->save();
                        }
                        $order_amount+=$_op->amount;
                        break;
                    case 'activity':
                        $pa=ProductActivity::find($_op->product_id);
                        if($pa->type==1){
                            $opacp=MirrorOrderProductAcPackage::where("order_product_id",$_op->id)->get();
                            foreach($opacp as $_opacp){
                                $package_amount=0;//企业包中单个包的价格
                                $item=MirrorOrderProductAcItem::where('order_product_package_id',$_opacp->id)->get();
                                foreach ($item as $key=>$_item) {
                                    if($_item->type==1){
                                        $package_amount+=$_item->amount*$_item->num;
                                        $order_product_amount+=$_item->amount*$_item->num;
                                    }else{
                                        $package_amount+=$_item->amount;
                                        $order_product_amount+=$_item->amount;
                                    }
                                }
                                $_opacp->amount=$package_amount;
                                $_opacp->save();
                            }
                            $order_amount+=$_op->amount;
                        }else{
                            $opacp=MirrorOrderProductAcPackage::where("order_product_id",$_op->id)->get();
                            foreach($opacp as $_opacp){
                                $package_amount=0;//活动包单个包的价格
                                $item=MirrorOrderProductAcItem::where('order_product_package_id',$_opacp->id)->get();
                                foreach ($item as $key=>$_item) {
                                    if($_item->type==1){
                                        $package_amount+=$_item->amount*$_item->num;
                                        $order_product_amount+=$_item->amount*$_item->num;
                                        $order_product_discount+=$_item->num*$_item->amount*(1-$_item->discount);
                                        $order_amount+=$_item->amount*$_item->num;
                                    }else{
                                        $package_amount+=$_item->amount;
                                        $order_product_amount+=$_item->amount;
                                        $order_product_discount+=$_item->amount*(1-$_item->discount);
                                        $order_amount+=$_item->amount;
                                    }
                                }
                                $_opacp->amount=$package_amount;
                                $_opacp->save();
                            }
                        }
                        break;
                    case 'promotion':
                        $opp=MirrorOrderProductPackage::where("order_product_id",$_op->id)->get();
                        foreach($opp as $_opp){
                            $package_amount=0;//推广包中单个包的价格
                            $item=MirrorOrderProductEsItem::where('order_product_package_id',$_opp->id)->get();
                            foreach ($item as $key=>$_item) {
                                if($_item->type==1){
                                    $package_amount+=$_item->amount*$_item->num;
                                    $order_product_amount+=$_item->amount*$_item->num;
                                    $order_amount+=$_item->amount*$_item->num;
                                }else{
                                    $package_amount+=$_item->amount;
                                    $order_product_amount+=$_item->amount;
                                    $order_amount+=$_item->amount;
                                }
                            }
                            $_opp->amount=$package_amount;
                            $_opp->save();
                        }
                        break;
                    case 'base':
                        $item=MirrorOrderProductBaseItem::where("order_product_package_id",$_op->id)->get();
                        foreach ($item as $key=>$_item) {
                            if($_item->type==1){
                                $order_product_amount+=$_item->amount*$_item->num;
                                $order_product_discount+=$_item->num*$_item->amount*(1-$_item->discount);
                                $order_amount+=$_item->amount*$_item->num;
                            }else{
                                $order_product_amount+=$_item->amount;
                                $order_product_discount+=$_item->amount*(1-$_item->discount);
                                $order_amount+=$_item->amount;
                            }
                        }
                        break;
                    case 'non_standard':
                        $item=MirrorOrderProductNonStandard::where("order_product_id",$_op->id)->where('status',1)->get();
                        foreach ($item as $key=>$_item) {
                            if($_item->service_type==1){
                                $order_product_amount+=$_item->amount*$_item->num;
                                $order_product_discount+=$_item->num*$_item->amount*(1-$_item->discount);
                                $order_amount+=$_item->amount*$_item->num;
                            }else{
                                $order_product_amount+=$_item->amount;
                                $order_product_discount+=$_item->amount*(1-$_item->discount);
                                $order_amount+=$_item->amount;
                            }
                        }
                        break;
    
                }
                $order_discount+=$order_product_discount;//每个产品包优惠相加得到订单总优惠
    
                if($_op->product_type!='enterprise'){
                    $_op->discount=$order_product_discount;
                    if($_op->product_type=='activity'){
                        $pa=ProductActivity::find($_op->product_id);
                        if($pa->type==2){
                            $_op->amount=$order_product_amount;
                        }
                    }else{
                        $_op->amount=$order_product_amount;
                    }
                    $_op->save();
                }
            }
            $order->discount=$order_discount;
            $order->balance_amount=$order_amount-$order->discount_amount-$order_discount;
            $order->amount=$order_amount;
            $order->save();
        });
    }
}